package com.snake.vargoappstore.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.snake.mylibrary.base.BaseFragment;
import com.snake.mylibrary.loader.ArrayListAdapter;
import com.snake.mylibrary.loader.DynamicResult;
import com.snake.mylibrary.utils.LogUtil;
import com.snake.vargoappstore.R;
import com.snake.vargoappstore.db.AppInfo;
import com.snake.vargoappstore.loader.HomeFragmentLoader;
import com.snake.vargoappstore.result.HomeFragmentResult;
import com.snake.vargoappstore.widget.ListViewItem;


public class HomeFragment extends BaseFragment{

    private View mView;
    private ListView mListView;
    private HomeFragmentListAdapter mHomeFragmentListAdapter;
    private FloatingActionButton mFlBtn;
    private HomeFragmentLoaderCallBacks mCallback;
    private ProgressBar mPbHome;

    @Override
    protected void initView() {
        mListView = (ListView) mView.findViewById(R.id.home_fragment_lv);
        mFlBtn = (FloatingActionButton) mView.findViewById(R.id.home_fab);
        mPbHome = (ProgressBar) mView.findViewById(R.id.pb_home);
    }

    @Override
    protected void initData() {
        mHomeFragmentListAdapter = new HomeFragmentListAdapter(getActivity());
        mListView.setAdapter(mHomeFragmentListAdapter);
        mCallback = new HomeFragmentLoaderCallBacks();
        initHomeLoader();
    }

    private void initHomeLoader() {
        getLoaderManager().initLoader(1001,null, mCallback);
    }
    private void restartHomeLoader(){
        getLoaderManager().restartLoader(1001,null,mCallback);
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.e("onResume");
    }

    @Override
    protected void initListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "position: "+position, Toast.LENGTH_SHORT).show();
            }
        });
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState){
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        boolean nextPage = view.getLastVisiblePosition() == view.getCount() - 1;
                        if(nextPage){
                            Toast.makeText(getActivity(),"loading more ",Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
               if(view.getLastVisiblePosition()==totalItemCount-1) {

               }
            }
        });
        mFlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restartHomeLoader();
            }
        });
    }

    @Override
    protected View getFragmentView() {
        LogUtil.e("HomeFragment");
        mView = View.inflate(getActivity(), R.layout.home_fragment, null);
        return mView;
    }

    class HomeFragmentListAdapter extends ArrayListAdapter<AppInfo> {

        public HomeFragmentListAdapter(Context context) {
            super(context);
        }

        @Override
        public View onCreateView(Context context, ViewGroup parent) {
            return new ListViewItem(context);
        }

        @Override
        public void onBindData(View view, int position, AppInfo data) {
            ((ListViewItem)view).bindData(position,data);
        }
    }

    class HomeFragmentLoaderCallBacks implements LoaderManager.LoaderCallbacks<DynamicResult> {


        @Override
        public Loader<DynamicResult> onCreateLoader(int id, Bundle args) {
            mPbHome.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            return new HomeFragmentLoader(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<DynamicResult> loader, DynamicResult data) {
            HomeFragmentResult result = (HomeFragmentResult) data;
            mHomeFragmentListAdapter.updateData(result.getAppInfos());
            mPbHome.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoaderReset(Loader<DynamicResult> loader) {

        }
    }
}
